package com.syf.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syf.domain.SysUser;
import com.syf.domain.store.BsCompanyApply;
import com.syf.model.Mails;
import com.syf.rocker.Source;
import com.syf.service.BsCompanyApplyService;
import com.syf.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syf.mapper.BsCompanyAuditMapper;
import com.syf.domain.store.BsCompanyAudit;
import com.syf.service.BsCompanyAuditService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MimeTypeUtils;

import java.util.UUID;

/**
*
*
*@author syf
*@create 2022-02-07 19:09
*/
@Service
@Slf4j
public class BsCompanyAuditServiceImpl extends ServiceImpl<BsCompanyAuditMapper, BsCompanyAudit> implements BsCompanyAuditService{

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private BsCompanyApplyService bsCompanyApplyService;

    @Autowired
    private Source source;



    /**
     * 审核软件厂商:
     *      添加一个审核记录
     *      修改申请表(厂商)状态
     *      创建一个用户，设置权限
     *      使用MQ异步发生邮箱给厂商
     *
     * @param bsCompanyAudit
     * @return
     */
    @Override
    @Transactional
    public boolean auditCompany(BsCompanyAudit bsCompanyAudit) {
        Integer num = RandomUtil.randomInt(1,99999999);
        bsCompanyAudit.setId(num);
        //添加一个审核记录
        boolean save = save(bsCompanyAudit);
        if(!save){
            return false;
        }

        //修改申请表状态
        BsCompanyApply companyApply = bsCompanyApplyService.getById(bsCompanyAudit.getApplyId());
        companyApply.setAuditId(num);
        if(bsCompanyAudit.getStatus() == 1) { //审核--不通过
            companyApply.setApplyStatus(2);  //申请--失败

            //使用MQ异步发生邮箱给厂商
            Mails mails = new Mails(companyApply.getQq()+"@qq.com",
                    "申请入驻失败",
                    bsCompanyAudit.getAuditDesc());
            MessageBuilder<Mails> mailMessageBuilder = MessageBuilder
                    .withPayload(mails)
                    .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
            boolean send = source.mailOut().send(mailMessageBuilder.build());

            log.info("MQ 产生消息:{}---{}入驻",send,companyApply.getCompanyName());
        }else { //审核--通过  0
            companyApply.setApplyStatus(1);//申请--成功
            companyApply.setStatus(0);  //企业状态--正常

            //创建一个用户，设置权限
            SysUser sysUser = new SysUser();
            sysUser.setCompanyId(bsCompanyAudit.getApplyId());   //对应厂商
            sysUser.setUsername(companyApply.getCompanyCode());
            sysUser.setPassword("123456");   //默认密码
            sysUser.setMobile(companyApply.getCompanyPhone());
            sysUser.setEmail(companyApply.getQq());
            sysUser.setFullname(companyApply.getCompanyName());
            sysUser.setStatus((byte) 1);
            sysUser.setRole_strings("1017767747970568195");   //role_id  软件厂商角色
            boolean isSuccess = sysUserService.addUser(sysUser);
            if(isSuccess){
                //使用MQ异步发生邮箱给厂商
                Mails mails = new Mails(companyApply.getQq()+"@qq.com",
                        "欢迎 "+bsCompanyAudit.getCompanyName()+" 成功入驻bs软件应用商店",
                        "贵司您好，我们将为您提供一个后台内部账号，后台网址： "+"http://bs-store-manage:9527/#/login"+ ",账号： "+sysUser.getUsername()+" ,初始密码： 123456" );
                MessageBuilder<Mails> mailMessageBuilder = MessageBuilder
                        .withPayload(mails)
                        .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
                boolean send = source.mailOut().send(mailMessageBuilder.build());
                System.out.println("to:"+companyApply.getQq()+"@qq.com");
                log.info("MQ 产生消息:{}---{}入驻",send,companyApply.getCompanyName());
            }
        }
        bsCompanyApplyService.updateById(companyApply);
        return true;
    }

    /**
     * 查询软件厂商审核列表
     *
     * @param page
     * @param companyName
     * @param softwareName
     * @param status
     * @return
     */
    @Override
    public Page<BsCompanyAudit> findByPage(Page<BsCompanyAudit> page, String companyName, String softwareName, Integer status) {
        LambdaQueryWrapper<BsCompanyAudit> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(companyName),BsCompanyAudit::getCompanyName,companyName)
                .like(StringUtils.isNotEmpty(softwareName), BsCompanyAudit::getSoftwareName,softwareName)
                .eq(status != null, BsCompanyAudit::getStatus,status)
                .orderByDesc(BsCompanyAudit::getCreateTime);

        return page(page, wrapper);
    }
}
